﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CitiSurface
{
    /// <summary>
    /// Interaction logic for VideoPlayerControl.xaml
    /// </summary>
    public partial class VideoPlayerControl : UserControl
    {
        public static readonly DependencyProperty SourceProperty;

        public string Source
        {
            get { return (string) GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value);}
        }

        public static readonly DependencyProperty IsControlPanelEnabledProperty;

        public bool IsControlPanelEnabled
        {
            get { return (bool)GetValue(IsControlPanelEnabledProperty); }
            set { SetValue(IsControlPanelEnabledProperty, value);}
        }

        public static readonly RoutedEvent CloseClickEvent;

        public event RoutedEventHandler CloseClick
        {
            add { AddHandler(CloseClickEvent, value); }
            remove { RemoveHandler(CloseClickEvent, value);}
        }

        static VideoPlayerControl()
        {
            SourceProperty = DependencyProperty.Register("Source", typeof (string), typeof (VideoPlayerControl));

            IsControlPanelEnabledProperty = DependencyProperty.Register("IsControlPanelEnabled", typeof (bool),
                                                                        typeof (VideoPlayerControl), new UIPropertyMetadata(
                                                                            false));

            CloseClickEvent = EventManager.RegisterRoutedEvent("CloseClick", RoutingStrategy.Bubble,
                                                               typeof (RoutedEventHandler), typeof (VideoPlayerControl));
        }

        private DispatcherTimer _controlPanelTimer;

        public VideoPlayerControl()
        {
            InitializeComponent();
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(CloseClickEvent, this));
        }

        private void PlayButtonClick(object sender, RoutedEventArgs e)
        {
            VideoPlayer.Play();
            _controlPanelTimer.Start();
        }

        private void PauseButtonClick(object sender, RoutedEventArgs e)
        {
            VideoPlayer.Pause();
            _controlPanelTimer.Start();
        }

        private void RefreshButtonClick(object sender, RoutedEventArgs e)
        {
            VideoPlayer.Stop();
            VideoPlayer.Play();
            _controlPanelTimer.Start();
        }

        private void RootLoaded(object sender, RoutedEventArgs e)
        {
            VideoPlayer.Play();
        }

        private void VideoPlayerTouchDown(object sender, TouchEventArgs e)
        {
            ControlPanel.Visibility = Visibility.Visible;
            var controlPanelFadein = (Storyboard)FindResource("ControlPanelFadein");
            controlPanelFadein.Begin();
            if(_controlPanelTimer == null)
            {
                _controlPanelTimer = new DispatcherTimer();
                _controlPanelTimer.Interval = TimeSpan.FromSeconds(4);
                _controlPanelTimer.Tick += ControlPanelTimerTick;
            }
            _controlPanelTimer.Start();
        }

        void ControlPanelTimerTick(object sender, EventArgs e)
        {
            var controlPanelFadeout = (Storyboard)FindResource("ControlPanelFadeout");
            controlPanelFadeout.Begin();
            controlPanelFadeout.Completed += (s, t) => ControlPanel.Visibility = Visibility.Collapsed;
            _controlPanelTimer.Stop();
        }
    }
}
